package database;

import entity.ClassRoomBlock;
import manager.ClassRoomBlockManager;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Noi
 */
public class ClassRoomBlockManagerDB {

    private CallableStatement cstmt;
    private java.sql.Connection conn;
    private ResultSet rs;
    
    public ClassRoomBlockManagerDB(java.sql.Connection conn) {
        this.conn = conn;
    }
    
    public void loadData(ClassRoomBlockManager classRoomBlockManager) {
        try {
            cstmt = conn.prepareCall("{call sp_GetClassRoomBlock}");
            rs =  cstmt.executeQuery();
            while(rs.next()) {
                int classRoomBlockID = rs.getInt("ClassRoomBlockID");
                String className = rs.getString("ClassRoomBlockName");
                boolean status = rs.getBoolean("Status");
                ClassRoomBlock obj = new ClassRoomBlock(classRoomBlockID, className, status);
                classRoomBlockManager.add(obj);
            }
            rs.close();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(ClassRoomBlockManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void deleteClassRoomBlock(ClassRoomBlock obj) {
        try {
            cstmt = conn.prepareCall("{call sp_DeleteClassRoomBlock(?)}");
            cstmt.setInt(1, obj.getClassRoomBlockID());
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(ClassRoomBlockManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void insertClassRoomBlock(ClassRoomBlock obj) {
        try {
            cstmt = conn.prepareCall("{call sp_InsertClassRoomBlock(?,?)}");
            cstmt.registerOutParameter(1, Types.INTEGER);
            cstmt.setString(2, obj.getClassRoomBlockName());
            cstmt.executeUpdate();
            obj.setClassRoomBlockID(cstmt.getInt("ClassRoomBlockID"));
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(ClassRoomBlockManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void updateClassRoomBlock(ClassRoomBlock obj) {
        try {
            cstmt = conn.prepareCall("{call sp_UpdateClassRoomBlock(?,?,?)}");
            cstmt.setInt(1, obj.getClassRoomBlockID());
            cstmt.setString(2, obj.getClassRoomBlockName());
            cstmt.setBoolean(3, obj.isStatus());
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(ClassRoomBlockManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
